package com.IT.servletJsp.medicine;

import com.IT.service.AdminFunctionService;
import com.IT.service.Impl.AdminFunctionServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

@WebServlet(value = "/admin/updateMedicine")
public class updateMedicineJsp extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html; charset = utf-8");

        PrintWriter printWriter = response.getWriter();

        String mName = request.getParameter("mName");
        String nMName = request.getParameter("nMName");

        Date dNow = new Date();
        SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String updateDate = ft.format(dNow);

        AdminFunctionService adminFunctionService = new AdminFunctionServiceImpl();
        int flag = adminFunctionService.updateMedicine(mName, nMName, updateDate);

        if(flag == 1){
            //printWriter.print("修改成功！");
            //response.sendRedirect("/adminInterface.html");
            printWriter.print("<script>alert('修改成功！');window.location.href='/adminInterface.html'</script>");
        }
        else if(flag == -1){
            //printWriter.print("修改失败！");
            //药品不存在
            printWriter.print("<script>alert('药品不存在！');window.location.href='/updateMedicine.html'</script>");
        }
        else {
            //flag==0;数据库操作异常
            printWriter.print("<script>alert('数据库操作异常！');window.location.href='/updateMedicine.html'</script>");
        }
    }
}
